package com.terana.lbs.marketing.web.repository;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tarena.lbs.pojo.marketing.bo.CouponCodeBO;
import com.tarena.lbs.pojo.marketing.dos.CouponCodeDO;
import com.tarena.lbs.pojo.marketing.po.CouponCodePO;
import com.terana.lbs.marketing.web.mapper.CouponCodeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Repository
public class CouponCodeRepository extends ServiceImpl<CouponCodeMapper, CouponCodePO> {
    @Autowired
    private CouponCodeMapper couponCodeMapper;

    public CouponCodeBO getAvailableCouponCode(Integer couponId) {
        //select * from coupon_code where coupon_id=#{} and status=0
        List<CouponCodeDO> doList=couponCodeMapper.selectListByCouponId(couponId);
        if (CollectionUtils.isEmpty(doList)){
            return null;
        }else{
            CouponCodeDO couponCodeDO = doList.get(0);
            CouponCodeBO bo=new CouponCodeBO();
            BeanUtils.copyProperties(couponCodeDO,bo);
            return bo;
        }

    }

    public void updateReceivedById(CouponCodeBO couponCodeBO) {
        couponCodeMapper.updateReceivedById(couponCodeBO.getId());
    }
}
